package com.tansun.easycare.rule.exception;

import java.io.IOException;
import java.util.Properties;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

public class BizException extends RuntimeException {


    private static final long serialVersionUID = 6508629897435214810L;

    private String errorCode;

    private String message;

    private static Properties prop = null;

    private static final String errorFile = "/message.properties";

    public BizException(Throwable cause, String errorCode) {

        super(cause);
        if (BizException.prop == null) {              	
            Resource resource = new ClassPathResource(errorFile);
            try {
                BizException.prop = PropertiesLoaderUtils.loadProperties(resource);
            } catch (IOException e) {

                e.printStackTrace();
            }
        }
        this.errorCode = errorCode;
    }

    public BizException(String errorCode, String message) {
        this.errorCode = errorCode;
        this.message = message;
    }

    public BizException(String errorCode) {
        super();
        if (BizException.prop == null) {
            Resource resource = new ClassPathResource(errorFile);
            try {
                BizException.prop = PropertiesLoaderUtils.loadProperties(resource);
            } catch (IOException e) {

                e.printStackTrace();
            }
        }
        this.errorCode = errorCode;

    }

    public String getMessage() {
        if (this.message != null) {
            return this.message;
        }
        if (prop != null&&prop.getProperty(this.errorCode)!=null) {
        	try {
				this.message = new String(prop.getProperty(this.errorCode).getBytes("ISO8859-1"), "utf-8");
			} catch (Exception e) {
				e.printStackTrace();
			}
            return this.message;
        }
        return this.errorCode;
    }

}
